// ColorCombo.cpp : implementation file
//

#include "stdafx.h"
#include "ColorCombo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CColorCombo

CColorCombo::CColorCombo()
{
	m_bSubclassed = FALSE;
	m_bShowColorName = TRUE;
	m_lb.pcb = this;
}

CColorCombo::~CColorCombo()
{
}


BEGIN_MESSAGE_MAP(CColorCombo, CComboBox)
	//{{AFX_MSG_MAP(CColorCombo)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorCombo message handlers

HBRUSH CColorCombo::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	if(!m_bSubclassed)
	{
		m_lb.SubclassWindow(pWnd->GetSafeHwnd());
		m_lb.SetOwner(this);
		m_bSubclassed = TRUE;
	}
	
	// TODO: Return a different brush if the default is not desired
	HBRUSH hbr = CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
	return hbr;
}

void CColorCombo::DrawItem(LPDRAWITEMSTRUCT lpdis) 
{
	m_lb.DrawItem(lpdis);
}

int CColorCombo::AddItem(COLORREF rgbValue, LPCTSTR colorText)
{
	ColorComboItem item;
	item.color = rgbValue;
	item.text = colorText;

	m_arr.Add(item);
	CComboBox::AddString(NULL);
	return -1;
}

int CColorCombo::AddItem(COLORREF rgbValue, int nColorIndex/* = 0*/)
{
	ColorComboItem item;
	item.color = rgbValue;
	item.text.Format("%02d:", nColorIndex);

	m_arr.Add(item);
	CComboBox::AddString(NULL);
	return -1;
}

COLORREF CColorCombo::GetSelectedColor()
{
	int nCurSel = GetCurSel();
	if(nCurSel < 0)
	{
		return COLORREF(-1);
	}
	return m_arr.GetAt(nCurSel).color;
	
}

COLORREF CColorCombo::GetColor(int nIndex)
{
	return m_arr.GetAt(nIndex).color;
}

BOOL CColorCombo::SetSelectedColor(COLORREF color)
{
	for(int n = 0; n < m_arr.GetSize(); n++)
	{
		if(m_arr[n].color == color)
		{
			SetCurSel(n);
			return TRUE;
		}
	}
	return FALSE;
}

void CColorCombo::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	ASSERT(FALSE);
}

void CColorCombo::SetTextWidth(int textWidth)
{
	m_lb.m_textWidth = textWidth;
}

void CColorCombo::SetColor(int nIndex, COLORREF color)
{
	ColorComboItem cci = m_arr.GetAt(nIndex);
	cci.color = color;
	m_arr.SetAt(nIndex, cci);
	Invalidate(FALSE);
	UpdateWindow();
}